package com.zx.BookSeaReader.greendao.service;

import android.database.Cursor;

import com.zx.BookSeaReader.greendao.GreenDaoManager;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.gen.UserBeanDao;

import java.util.List;
import java.util.UUID;

/**
 * @Author: ZX
 * @Time: 2020/3/25
 * @Description: 功能描述
 */
public class UserService extends BaseService{

    /**
     * @Author: ZX
     * @Time: 2020/2/15
     * @Description: 查询所有用户
     */
    public List<UserBean> getAllUsers(){
        UserBeanDao userBeanDao = GreenDaoManager.getInstance().getSession().getUserBeanDao();
        List<UserBean> Users=userBeanDao.loadAll();
        return Users;

    }
    /**
     * @Author: ZX
     * @Time: 2020/2/15
     * @Description: 该用户是否存在
     */
    public boolean existUser(String username){
        UserBeanDao userBeanDao = GreenDaoManager.getInstance().getSession().getUserBeanDao();
        List<UserBean> Users=userBeanDao.queryBuilder()
                .where(UserBeanDao.Properties.Username.eq(username))
                .list();
        if(Users.size()>0){
            return true;
        }else {
            return false;
        }

    }
    /**
     * @Author: ZX
     * @Time: 2020/2/15
     * @Description: 该用户是否存在
     */
    public boolean checkUser(String username,String password){
        UserBeanDao userBeanDao = GreenDaoManager.getInstance().getSession().getUserBeanDao();
        List<UserBean> Users=userBeanDao.queryBuilder()
                .where(UserBeanDao.Properties.Username.eq(username),UserBeanDao.Properties.Password.eq(password))
                .list();
        if(Users.size()>0){
            return true;
        }else {
            return false;
        }

    }

    /**
     * @Author: ZX
     * @Time: 2020/2/15
     * @Description: 该用户是否存在
     */
    public UserBean getUser(String username){
        UserBeanDao userBeanDao = GreenDaoManager.getInstance().getSession().getUserBeanDao();
        return userBeanDao.queryBuilder()
                .where(UserBeanDao.Properties.Username.eq(username))
                .unique();

    }

    public void addUserBean(UserBean userBean){
        addEntity(userBean);
    }

    /**
     * @Author: ZX
     * @Time: 2020/2/15
     * @Description: 该用户是否存在
     */
    public boolean findUserByName(String username){
        boolean result=false;
        try {
            Cursor cursor = selectBySql("select id from user_bean where username = ? ",new String[]{username});
            if (cursor.moveToNext()){
                result=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }





}
